package com.hdp.jetpack_mvvm

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.hdp.jetpack_mvvm.lifecycle.LifecycleActivity1
import com.hdp.jetpack_mvvm.livedata.SingletonLiveData
import com.hdp.jetpack_mvvm.livedata.LiveDataActivity1
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class MainActivity : AppCompatActivity() {
    companion object {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        init()
    }

    fun init() {
        btn_life_act.setOnClickListener {
            startActivity(Intent(this, LifecycleActivity1::class.java))
        }
        btn_viewmodel_act.setOnClickListener {
            startActivity(Intent(this, LiveDataActivity1::class.java))

        }
    }

    override fun onResume() {
        super.onResume()
        //全局的LiveData，单例，在Activity共享数据
        GlobalScope.launch(Dispatchers.Main) {
            delay(500)
            SingletonLiveData.setName("张三001")
        }
    }
}